package itheima_edu_info_manager.com.itheima.edu.info.manager.dao;

import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao extends BaseStudentDao{
    //将数组变为成员变量，并用static进行修饰（将此数组变成公共数组）
    private static ArrayList<Student> stus = new ArrayList<>();

    static {
        stus.add(new Student("heima001","张三","19","1997-11-11"));
        stus.add(new Student("heima002","李四","22","1990-11-11"));
    }

    //添加学生
    public boolean addStudent(Student stu) {
        stus.add(stu);
        return true;
    }

    //查看学生
    public Student[] findAllStudent() {
        Student[] students = new Student[stus.size()];
        for (int i = 0; i < students.length; i++) {
            Student student = stus.get(i);
            students[i] = student;
        }
        return students;
    }

    //删除学生
    public void deleteStudentById(String delid) {
        int index = getIndex(delid);
        stus.remove(index);
    }

    //获取对应ID的索引编号
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            Student st = stus.get(i);
            if (st != null && st.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //修改学生
    public void upDateStudentById(String id, Student stu) {
        int index = getIndex(id);
        stus.set(index,stu);
    }
}
